package com.jvm123.excel.in.impl;

import com.jvm123.excel.common.ExcelProperties;
import com.jvm123.excel.in.ExcelHelper;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import java.util.ArrayList;
import java.util.List;

public class DefaultExcelHelper implements ExcelHelper {

    private ExcelProperties excelProperties;

    public DefaultExcelHelper() {
    }

    public DefaultExcelHelper(ExcelProperties excelProperties) {
        this.excelProperties = excelProperties;
    }

    @Override
    public List<String> rowToList(Row row) {
        List<String> list = new ArrayList<>();
        if (row != null) {
            // 列数
            int columnNum = row.getPhysicalNumberOfCells();
            for (int i = 0; i < columnNum; i++) {
                Cell cell = row.getCell(i);
                String content = cell.getStringCellValue();
                list.add(i, content);
            }
        }
        return list;
    }

    @Override
    public List<String> rowToList(Sheet sheet, int rowIndex) {
        Row row = sheet.getRow(rowIndex);
        return rowToList(row);
    }

    @Override
    public List<String> rowToList(Workbook workbook, int sheetIndex, int rowIndex) {
        Sheet sheet = workbook.getSheetAt(sheetIndex);
        return rowToList(sheet, rowIndex);
    }

    @Override
    public List<String> getCommentList(Row row) {
        return rowToList(row);
    }

    @Override
    public List<String> getCommentList(Sheet sheet) {
        return rowToList(sheet, excelProperties.getCommentRowIndex());
    }

    @Override
    public List<String> getCommentList(Workbook workbook) {
        return rowToList(workbook, excelProperties.getSheetIndex(), excelProperties.getCommentRowIndex());
    }

    @Override
    public List<String> getNameList(Row row) {
        return rowToList(row);
    }

    @Override
    public List<String> getNameList(Sheet sheet) {
        return rowToList(sheet, excelProperties.getNameRowIndex());
    }

    @Override
    public List<String> getNameList(Workbook workbook) {
        return rowToList(workbook, excelProperties.getSheetIndex(), excelProperties.getNameRowIndex());
    }
}
